<?php
/**
 * @file			mod.basic.php
 * @CopyRight		(C)1996-2099 SINA Inc.
 * @Project			vyouji
 * @Author			guanghui <guanghui1@staff.sina.com.cn>
 * @Create Date:	2011-10-09
 * @Modified By:	guanghui/2011-10-09
 * @Brief			管理后台数据库操作基类
 */

class modBasic extends modules_base
{
	/**
	* 数据表名，无前缀，对应config.php中定义的数据表常量
	* 
	* @var string
	*/
	protected $_t_name;
	
	/**
	* 完整的数据表名
	* 
	* @var string
	*/
	protected $_table;
	
	/**
	* 数据库操作句柄对象
	* 
	* @var object
	*/
	protected $_db;
	
	/**
	* 构造函数
	* 
	* @param string $tname 不带前缀的数据表名
	* @return adminModBasic
	*/
	public function __construct($tname, $is_single = true)
	{
		$this->_db = APP::ADP('db', $is_single);
		$this->setTable($tname);
	}
	
	/**
	 * @param $data array|array|int
	 * @param $key string|int 数组下标或默认值(如果$data不是数组)
	 * @param $default int 默认值
	 * @return int 
	 */
	function getInt($data, $key=null, $default=null) {
		if (is_array($data) && $key && isset($data[$key])) {
			$d = $data[$key];
		} else {
			$d = $data;
			$default = $key;
		}
		if (!is_numeric($d) && !is_int($d)) {
			return $default === null ? false :$default;
		}
		return (int)$d;
	}

	/**
	 * @param $data array|string(number)
	 * @param $default string 默认值
	 * @return string
	 */
	function getString($data, $key=null, $default=null) {
		if (is_array($data) && $key && isset($data[$key])) {
			$d = $data[$key];
		} else {
			$d = $data;
			$default = $key;
		}
		if (!is_string($d)) {
			return $default === null ? false :$default;
		}
		return (string)$d;
	}
	
	/**
	 * 得到一数组,如果是字串，则尝试用","切割
	 * @param $data mixed
	 * @return array
	 */
	function getArray($data, $key=null, $default=null) {
		if (is_array($data) && $key && isset($data[$key])) {
			$d = $data[$key];
		} else {
			$d = $data;
			$default = $key;
		}
		if (is_string($d)) {
			$d = explode(',', $data);
		}
		return (array)$d;
	}

	/**
	 * @param $data array|string(number)
	 * @param $key string 数组下标或默认值(如果$data不是数组)
	 * @param $default int 默认值
	 * @return string(number) 
	 */
	function getNumeric($data, $key=null, $default=null) {
		if (is_array($data) && $key && isset($data[$key])) {
			$d = $data[$key];
		} else {
			$d = $data;
			$default = $key;
		}
		if (!is_numeric($d)) {
			return $default === null ? false :$default;
		}
		return $d;
	}
	
	public function setTable($tname){
		$this->_t_name = $tname;
		$this->_table = $this->_db->getTable($tname);
	}
	/**
	* 获取数据列表
	* 
	* @param array $condition 精确匹配的条件数组
	* @param array $like 模糊匹配的条件数组
	* @param array $order 排序规则数组
	* @param int $offset 偏移量
	* @param int $limit 数据限额
	*/
	public function getList($condition = array(), $like = array(), $order = array(), $offset = 0, $limit = 0)
	{
		$sql = "SELECT * FROM `" . $this->_table . "` WHERE 1=1";
		if (is_array($condition) && !empty($condition)) {
			foreach ($condition as $key => $value) {
				$key = trim($key);
				$value = trim($value);
				if ($key === '' || $value === '')
					continue;
				$sql .= $this->_db->SQL(" AND `%s` = '%s'", $key, $value);
			}
		}
		if (is_array($like) && !empty($like)) {
			foreach ($like as $key => $value) {
				$key = trim($key);
				$value = trim($value);
				if ($key === '' || $value === '')
					continue;
				$sql .= $this->_db->SQL(" AND `%s` LIKE '%%s%'", $key, $value);
			}
		}
		if (is_array($order) && !empty($order)) {
			$sqlOrder = '';
			foreach ($order as $key => $value) {
				$key = trim($key);
				$value = strtoupper(trim($value));
				if ($key === '' || $value === '' || !in_array($value, array('ASC', 'DESC')))
					continue;
				$sqlOrder .= $this->_db->SQL("`%s` %s,", $key, $value);
			}
			$sql .= $sqlOrder === '' ? $sqlOrder : ' ORDER BY ' . rtrim($sqlOrder, ',');
		}
		if ($limit) {
			$sql .= $this->_db->SQL(" LIMIT %d, %d", $offset, $limit);
		}
		return RST($this->_db->query($sql));
	}
	
	/**
	* 获取满足条件的条目数据
	* 
	* @param array $condition 精确匹配的条件数组
	* @param array $like 模糊匹配的条件数组
	*/
	public function getNum($condition = array(), $like = array())
	{
		$sql = "SELECT COUNT(*) FROM `" . $this->_table . "` WHERE 1=1";
		if (is_array($condition) && !empty($condition)) {
			foreach ($condition as $key => $value) {
				$sql .= $this->_db->SQL(" AND `%s` = '%s'", $key, $value);
			}
		}
		if (is_array($like) && !empty($like)) {
			foreach ($like as $key => $value) {
				$sql .= $this->_db->SQL(" AND `%s` LIKE '%%s%'", $key, $value);
			}
		}
		return RST($this->_db->getOne($sql));
	}
	
	/**
	* 保存一条数据记录
	* 
	* @param array $data
	* @param int $id
	* @param string $field
	*/
	public function save($data, $id = '', $field = 'id')
	{
		return RST($this->_db->save($data, $id, $this->_t_name, $field));
	}
	
	/**
	* 根据指定的条件获取一条记录
	* 
	* @param array $condition
	*/
	public function getOneRow($condition)
	{
		$sql = "SELECT * FROM `" . $this->_table . "` WHERE 1=1";
		if (is_array($condition) && !empty($condition)) {
			foreach ($condition as $key => $value) {
				$key = trim($key);
				$value = trim($value);
				if ($key === '')
					continue;
				$sql .= $this->_db->SQL(" AND `%s` = '%s'", $key, $value);
			}
		}
		return RST($this->_db->getRow($sql));
	}
	
	/**
	* 多条件组合保存方式
	* 
	* @param array $data 保存的数据
	* @param array $condition  更新数据条件
	*/
	public function saveByCondition($data, $condition = array())
	{
		if (!empty($condition)) {
			$sql = "UPDATE `%s` SET";
		} else {
			$sql = "INSERT INTO `%s` SET";
		}
		$sql = $this->_db->SQL($sql, $this->_table);
		
		foreach ($data as $key => $value) {
			$key = trim($key);
			$value = trim($value);
			if ($key === '')
				continue;
				
			$sql .= $this->_db->SQL(" `%s` = '%s',", $key, $value);
		}
		$sql = rtrim($sql, ',');
		
		if (!empty($condition)) {
			$where = array();
			foreach ($condition as $key => $value) {
				$key = trim($key);
				$value = trim($value);
				if ($key === '')
					continue;
				
				$where[] = $this->_db->SQL("`%s` = '%s'", $key, $value);
			}
			$sql .= " WHERE " . implode(' AND ', $where);
		}
		
		return RST($this->_db->execute($sql));
	}
}
